const app = getApp()
import request from '../../utils/request.js';
import tool from '../../utils/throttle.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jinelist: '',
    jine: '',
    type: 0,
    ymurl: app.globalData.ymurl,
    tixianBtn: true,
    nojine: 0,
    showdialog: false, //控制弹窗
    name: '',
    show: false,
    zfbnum: 0,
    isShow: true,
    accountZfb: "",
    showval: false,
    VerificationCode: "获取验证码",
    sms: '', //验证码
    currentTime: 60, //验证码描述
    disabled: false,
    sendCode: "",
    countdown1: "",
    zfbshow: "",
    zfbAccount: "",
    balance: 0,
    accountShow: true,
    accountZfb1: "",
    intvalue: '',
    interval: null,
    commission: 0,
    withdrawalRateCheck: '',
    roleId: '',
    extraData: [],
  },
  skinGo() {
    wx.navigateTo({
      url: '../recordList/recordList',
    })
  },
  skinGo1() {
    wx.navigateTo({
      url: '../handlingCharge/handlingCharge',
    })
  },
  clickWxbox(e) {
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        isShow: true,
        type: 0,
      })
    } else {
      //获取支付宝账号
      request({
        url: '/agent/get/zfb/account',
        method: "GET"
      }).then(res => {

        if (res.data.data) {
          this.setData({
            show: false,
            accountZfb1: res.data.data
          })
        } else {
          this.setData({
            show: true
          })
        }
      })
      this.setData({
        isShow: false,
        type: 1,
      })
    }
  },
  closeBox() {
    this.setData({
      show: false
    })
    wx.showToast({
      title: '拒绝输入将无法支付宝提现',
      icon: "none",
      duration: 3000,
      mask: true
    })
  },
  closezfbBox() {
    this.setData({
      zfbshow: false
    })
  },
  inputchangeZfb(e) {
    // console.log(e.detail.value);
    this.setData({
      zfbAccount: e.detail.value
    })
  },
  opnenzfbBox() {
    request({
      url: "/agent/change/zfb/account/" + this.data.zfbAccount,
    }).then(res => {
      wx.showToast({
        title: res.data.message,
        icon: 'none',
        duration: 3000
      })
      if (res.data.code == 200) {
        this.setData({
          zfbshow: false,
          accountZfb1: res.data.data
        })

      }
    })
  },
  opnenBox() {
    if (this.data.accountZfb) {
      request({
        url: "/agent/change/zfb/account/" + this.data.accountZfb,
      }).then(res => {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 3000
        })
        if (res.data.code == 200) {
          this.setData({
            show: false,
            accountZfb1: this.data.accountZfb
          })
        }
      })

    } else {
      wx.showToast({
        title: '请输入支付宝账号',
        icon: "none",
        duration: 3000,
        mask: true
      })
    }
  },
  inputZfb(e) {
    this.setData({
      accountZfb: e.detail.value
    })
  },
  sendCode(e) {
    this.setData({
      sendCode: e.detail.value
    })
  },
  inputsend(e) {
    this.setData({
      sendCode: e.detail.value
    })
  },
  closesmallBox() {
    this.setData({
      showval: false,
      sendCode: ""
    })
  },
  changeZfb() {

    this.setData({
      zfbshow: true
    })
  },
  sms(e) {
    this.setData({
      sms: e.detail,
    })
  },
  opnensmallBox: tool.throttle(function (msg) {
    const that = this
    console.log(1);
    //提现按钮
    if (that.data.roleId == 0) {
      console.log(123);
      if (that.data.sms == '') {
        wx.showToast({
          title: '请先输入验证码',
          icon: 'none',
          duration: 2500,
        })
      } else {
        that.setData({
          showval: false,
        })
        wx.showLoading({
          title: '请等待~',
          mask: true,
        })
        request({
          url: '/admin/withdrawals',

          // 大额提现
          // url: '/agent/withdrawals/1',
          data: {
            amount: that.data.intvalue,
            type: 0,
            openid: wx.getStorageSync('userInfo').openid,
            oldOpenid: that.data.extraData.openid,
            signCode: that.data.sms
          }
        }).then(res => {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            duration: 3000,
            mask: true
          })
          if (res.data.code == 200) {
            that.setData({
              sendCode: "",
              disabled: false,
              VerificationCode: "获取验证码",
            })
            clearInterval(that.data.interval);
          }
        })
      }
    } else if (that.data.roleId == 1 || that.data.roleId == 2 || that.data.roleId == 3) {
      if (that.data.type === 0) {
        if (that.data.sms == '') {
          wx.showToast({
            title: '请先输入验证码',
            icon: 'none',
            duration: 2500
          })
        } else {
          that.setData({
            showval: false,
          })
          wx.showLoading({
            title: '请等待~',
            mask: true,
          })
          request({
            url: "/agent/withdrawals",

            // 大额提现
            // url: that.data.intvalue>500?'/agent/withdrawals/1':'/agent/withdrawals',
            data: {
              // amount: that.data.intvalue,
              // type: that.data.type,
              // openid: wx.getStorageSync('userInfo').openid,
              // oldOpenid: that.data.extraData.openid,
              // signCode: that.data.sms
              amount: that.data.intvalue,
              type: that.data.type || 0,
              openid: wx.getStorageSync('userInfo').openid,
              oldOpenid: 'otIIa5EjFofqiQUipt8x0wr9Ah_g',
              signCode: that.data.sms
            }
          }).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: res.data.message,
              icon: "none",
              duration: 3000,
              mask: true
            })
            if (res.data.code == 200) {
              that.setData({
                sendCode: "",
                disabled: false,
                VerificationCode: "获取验证码",
              })
              clearInterval(that.data.interval);

            }
          })
        }
      } else {
        if (that.data.sms == '') {
          wx.showToast({
            title: '请先输入验证码',
            icon: 'none',
            duration: 2500
          })
        } else {
          that.setData({
            showval: false,
          })
          wx.showLoading({
            title: '请等待~',
            mask: true,
          })
          request({
            // url: that.data.intvalue>500?'/agent/withdrawals/1':'/agent/withdrawals',

            url: "/agent/withdrawals",
            data: {
              amount: that.data.intvalue,
              type: that.data.type,
              signCode: that.data.sms
            }
          }).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: res.data.message,
              icon: "none",
              duration: 3000,
              mask: true
            })
            if (res.data.code == 200) {
              that.setData({
                sendCode: "",
                disabled: false,
                VerificationCode: "获取验证码",
              })
              clearInterval(that.data.interval);

            }
          })
        }
      }

    } else if (that.data.roleId == 4) {
      if (that.data.sms == '') {
        wx.showToast({
          title: '请先输入验证码',
          icon: 'none',
          duration: 2500
        })
      } else {
        that.setData({
          showval: false,
        })
        wx.showLoading({
          title: '请等待~',
          mask: true,
        })
        request({
          url: "/contact/site/withdraw",
          data: {
            openid: wx.getStorageSync('userInfo').openid,
            oldOpenid: that.data.extraData.openid,
            amount: this.data.intvalue,
            type: this.data.type,
            signCode: this.data.sms,
          }
        }).then(res => {
          wx.hideLoading();
          console.log(res);
          if (res.data.code == 200) {
            this.setData({
              sendCode: "",
              showval: false
            })
            if (res.data.data) {
              wx.showToast({
                title: res.data.data,
                icon: "none",
                duration: 3000
              })
            } else {
              wx.showToast({
                title: res.data.message,
                icon: "none",
                duration: 3000
              })
            }
          } else {
            wx.showToast({
              title: res.data.message,
              icon: "none",
              duration: 3000
            })
          }
        })
      }
    }

  }),
  withdrawalList() {
    if (this.data.roleId == 0) {
      wx.navigateTo({
        url: '../adminWithdrawalsRecords/adminWithdrawalsRecords',
      })
    } else {
      wx.navigateTo({
        url: '../withdrawDeposit/withdrawDeposit',
      })
    }
  },
  sqtixian() {
    if (this.data.roleId === 0) {
      if (this.data.intvalue < 2 || this.data.intvalue > 20000) {
        wx.showToast({
          title: '提现的金额只能在2-20000元之间',
          icon: 'none',
          duration: 2000
        })
      } else {
        this.setData({
          showval: true
        })
      }
    } else {
      if (this.data.type === 0) {
        if (this.data.intvalue < 2 || this.data.intvalue > 20000) {
          wx.showToast({
            title: '提现的金额只能在2-20000元之间',
            icon: 'none',
            duration: 2000
          })
        } else {
          this.setData({
            showval: true
          })
        }
      } else {
        if (this.data.intvalue < 2 || this.data.intvalue > 20000) {
          wx.showToast({
            title: '提现的金额只能在2-20000元之间',
            icon: 'none',
            duration: 2000
          })
        } else {
          this.setData({
            showval: true
          })
        }
      }
    }
  },
  backBtn() {
    wx.navigateBackMiniProgram({
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  // clickCode(seconds) {
  //     let countdown = setInterval(() => {
  //         if (seconds > 0) {
  //             --seconds
  //             this.setData({
  //                 btnText: seconds + "s后获取",
  //             })
  //         } else {
  //             this.setData({
  //                 btnText: "获取验证码",
  //                 disabled: false,
  //             })
  //             clearInterval(countdown);
  //         }
  //     }, 1000);
  //     this.setData({
  //         countdown1: countdown
  //     })
  // },
  // btnSend: function () { //获取验证码
  //     this.setData({
  //         disabled: true,
  //     })
  //     request({
  //         url: '/SMS/send',
  //         data: {
  //             phone: this.data.phone,
  //             type: 2
  //         }
  //     }).then(res => {
  //         if(res.data.code==200){
  //             setTimeout(() => {
  //                 this.clickCode(60)
  //             }, 500)
  //         }
  //     })
  // },
  // 获取验证码
  getCode() {
    const that = this;
    var currentTime = that.data.currentTime
    this.setData({
      interval: setInterval(function () {
        currentTime--;
        that.setData({
          VerificationCode: currentTime + '秒'
        })
        if (currentTime <= 0) {
          clearInterval(that.data.interval)
          that.setData({
            VerificationCode: '重新发送',
            currentTime: 60,
            disabled: false
          })
        }
      }, 1000)
    })
  },
  getVerificationCode() {
    var that = this
    if (that.data.phone) {
      that.getCode();
      request({
        url: '/SMS/send',
        data: {
          phone: that.data.phone,
          type: that.data.roleId == 4 ? 3 : 2,
        },
      }).then(res => {

      })
      that.setData({
        disabled: true
      })
    } else {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    }
  },
  inputjine(e) {
    this.setData({
      intvalue: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      extraData: wx.getStorageSync('extraData'),
      roleId: wx.getStorageSync('extraData').roleId,
      phone: wx.getStorageSync('extraData').phone,
      intvalue: wx.getStorageSync('extraData').amount
    })
    console.log(this.data.extraData.roleId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */

})